

package com.kimo.shop.mp.comment.api.handler;

import cn.hutool.core.util.StrUtil;
import com.kimo.shop.common.util.Json;
import com.kimo.shop.mp.comment.model.WeixinKeywordresponse;
import com.kimo.shop.mp.comment.service.WeixinKeywordresponseService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.api.WxConsts.KefuMsgType;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author kimo-taozhouchuan
 */
@Component
@AllArgsConstructor
public class MsgHandler extends AbstractHandler {

    private WeixinKeywordresponseService weixinKeywordresponseService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {
        //关键字回复
        String content = wxMessage.getContent();
        logger.info("进行关键字回复，关键词为：" + content);
        List<WeixinKeywordresponse> keywordresponses = weixinKeywordresponseService.listWeixinKeywordresponse();
        if (StrUtil.isBlank(content)) {
            return null;
        }
        content = content.trim();

        // 查找可以匹配的关键字
        WeixinKeywordresponse response = null;
        for (WeixinKeywordresponse keywordresponse : keywordresponses) {
            String keyword = keywordresponse.getKeyword();
            Integer pptype = keywordresponse.getPptype();
            // 模糊匹配
            if (pptype == 1 && StrUtil.containsIgnoreCase(content, keyword)) {
                response = keywordresponse;
                break;
            }
            // 完全匹配
            else if (pptype == 2 && Objects.equals(content, keyword)) {
                response = keywordresponse;
                break;
            }
        }
        if (response == null) {
            return null;
        }
        String msgType = response.getMsgType();
        logger.info("wx handleTextMessage result:{}", response.getContent());
        if (KefuMsgType.VIDEO.equals(msgType)) {
            return WxMpXmlOutMessage.VIDEO().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (KefuMsgType.VOICE.equals(msgType)) {
            return WxMpXmlOutMessage.VOICE().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (WxConsts.KefuMsgType.IMAGE.equals(msgType)) {
            return WxMpXmlOutMessage.IMAGE().mediaId(response.getMediaId()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        } else if (KefuMsgType.NEWS.equals(msgType)) {
//            WxMpMaterialNews wxMpMaterialNews = Json.parseObject(response.getContent(), WxMpMaterialNews.class);
//            List<WxMpNewsArticle> articles = wxMpMaterialNews.getArticles();
            WxMpNewsArticle wxMpNewsArticle = Json.parseObject(response.getContent(), WxMpNewsArticle.class);
            WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
            item.setDescription(wxMpNewsArticle.getDigest());
            item.setPicUrl(wxMpNewsArticle.getThumbUrl());
            item.setTitle(wxMpNewsArticle.getTitle());
            item.setUrl(wxMpNewsArticle.getUrl());
//            List<WxMpXmlOutNewsMessage.Item> newsItems = articles.stream().map(article -> {
//                WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
//                item.setDescription(article.getDigest());
//                item.setPicUrl(article.getThumbUrl());
//                item.setTitle(article.getTitle());
//                item.setUrl(article.getUrl());
//                return item;
//            }).collect(Collectors.toList());
            return WxMpXmlOutMessage.NEWS().articles(Collections.singletonList(item)).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        }
        return WxMpXmlOutMessage.TEXT().content(response.getContent()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
    }
}
